<template>
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">

        <p-row>
		  <p-col  :span="24">
			<p-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'remark', validatorRules.remark]" placeholder="请输入备注"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="标准端需求编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input readOnly v-decorator="[ 'standardReqno', validatorRules.standardReqno]" placeholder="请输入标准端需求编码"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="产品编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<f-popup
						contextPath="basic-business"
						v-decorator="['mcode', validatorRules.mcode]"
						:trigger-change="true"
						org-fields="id,mcode,mname,sprc,mexplain"
						dest-fields="mid,mcode,mname,sprc,mexplain"
						code="wms_cpinfo"
						@callback="popupCallback"/>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="产品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input readOnly v-decorator="[ 'mname', validatorRules.mname]" placeholder="请输入产品名称"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="产品系列" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'mseries', validatorRules.mseries]" placeholder="请输入产品系列"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="标准端需求数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input-number   v-decorator="[ 'standardReqqty', validatorRules.standardReqqty]" placeholder="请输入标准端需求数量" style="width: 100%"/>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="需求说明" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'reqexplanation', validatorRules.reqexplanation]" placeholder="请输入需求说明"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="需求时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<j-date    placeholder="请选择需求时间" v-decorator="[ 'reqtime', validatorRules.reqtime]" :trigger-change="true" style="width: 100%"/>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="申请人" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<j-select-user-by-dep v-decorator="['applyBy', validatorRules.applyBy]" :trigger-change="true"/>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="申请时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<j-date    placeholder="请选择申请时间" v-decorator="[ 'applyTime', validatorRules.applyTime]" :trigger-change="true" style="width: 100%"/>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<f-switch  placeholder="String" v-decorator="['status']" field="status" dictCode="mes_standard_require_status" @callback="popupCallback"/>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="计划意见" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'planComment', validatorRules.planComment]" placeholder="请输入计划意见"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="表单类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<j-dict-select-tag  type="list" v-decorator="['tableType', validatorRules.tableType]" :trigger-change="true" dictCode="mes_standard_require_type" placeholder="请选择表单类型"/>
			</p-form-item>
		  </p-col>
        </p-row>


      </p-form>
    </p-spin>
  </p-modal>
</template>

<script>
  import { httpAction, putAction, api } from '@/api/manage'
  import fSwitch from '@/components/switch'
  import { SubmitOnchangMixin } from '@/mixins/SubmitOnchangMixin'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import JSelectUserByDep from '@/components/jeecgbiz/JSelectUserByDep'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: "MesEqStandardRequireModal",
    mixins: [SubmitOnchangMixin],
    components: {
      JDate,
      JSelectUserByDep,
      JDictSelectTag,
      fSwitch,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          remark: {rules: [
          ]},
          standardReqno: {rules: [
            {required: true, message: '请输入标准端需求编码!'},
          ]},
          mcode: {rules: [
            {required: true, message: '请输入产品编码!'},
          ]},
          mname: {rules: [
            {required: true, message: '请输入产品名称!'},
          ]},
          mseries: {rules: [
          ]},
          standardReqqty: {rules: [
            {required: true, message: '请输入标准端需求数量!'},
            {pattern:/^-?\d+$/, message: '请输入整数!'},
          ]},
          reqexplanation: {rules: [
          ]},
          reqtime: {rules: [
          ]},
          applyBy: {rules: [
          ]},
          applyTime: {rules: [
          ]},
          status: {rules: [
            {required: true, message: '请输入状态!'},
          ]},
          planComment: {rules: [
          ]},
          tableType: {rules: [
            {required: true, message: '请输入表单类型!'},
          ]},
        },
        url: {
          add: '/mes-equipment/auto/mesEqStandardRequire/add',
          edit: '/mes-equipment/auto/mesEqStandardRequire/edit',
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
        this.getRuleCodeValue();
      },
      getRuleCodeValue() {
        let param = {
          commonFormData: this.model,
          rules: [
            {"ruleCode": "mes_standard_reqno", "columnName": "standardReqno"},
            ]
        }

        putAction(api.ruleBaseURL, param).then(res => {
          // 执行成功，获取返回的值，并赋到页面上
          if (res.success) {
            console.log("生成的编码：" + res.data)
            const item = res.data;
            this.form.setFieldsValue(pick(item, Object.keys(item)))
          }
        })
      },

      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'createBy','createTime','updateBy','updateTime','remark','standardReqno','mcode','mname','mseries','standardReqqty','reqexplanation','reqtime','applyBy','applyTime','status','planComment','tableType'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
              that.confirmLoading = true;
              let httpurl = '';
              let method = '';
              let formData = Object.assign(this.model, values);
              this.formData = Object.assign(this.model, values);
              //新增触发
              if(!this.model.id){
                httpurl+=this.url.add;
                method = 'post';
                if(this.EnhanceJS.afterAdd){
                  //新增之前
                  let Obj = eval(this.EnhanceJS.afterAdd)
                  new Obj(this)
                }else{
                  console.log("表单提交数据",formData)
                  that.modalFrom(httpurl,formData,method)
                }
              }else{
                //编辑触发
                httpurl+=this.url.edit;
                method = 'put';
                if(this.EnhanceJS.afterEdit){
                 //编辑之前
                  let Obj = eval(this.EnhanceJS.afterEdit)
                  new Obj(this)
                }else{
                  console.log("表单提交数据",formData)
                  that.modalFrom(httpurl,formData,method)
                }
              }
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'createBy','createTime','updateBy','updateTime','remark','standardReqno','mcode','mname','mseries','standardReqqty','reqexplanation','reqtime','applyBy','applyTime','status','planComment','tableType'))
      },


    }
  }
</script>